import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:learning_content_admin_pannel.core/core/presentation/theme/styles.dart';
import 'package:learning_content_admin_pannel/app/infrastructure/services/flavor_service/service.dart';
import 'package:learning_content_admin_pannel/app/presentation/widget/widget.dart';
import 'package:learning_content_admin_pannel/injection_container.dart' as di;
import 'package:learning_content_admin_pannel.core/core/domain/config/config.dart';
import 'package:learning_content_admin_pannel.core/logger/logger.dart';
import 'package:window_manager/window_manager.dart';
import 'package:window_size/window_size.dart' as window_size;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Future.wait([
    () async {
      final flavor = await const FlavorService().getFlavor();

      if (flavor == 'prod') {
        await Config().initProd();
      } else if (flavor == 'dev') {
        await Config().initDev();
      }

      Logger.logLevel = Config().logLevel;

      await di.init();
    }(),
    () async {
      final futureResults = await Future.wait([
        window_size.getWindowInfo(),
        () async {
          await windowManager.ensureInitialized();

          await Future.wait([
            windowManager.setTitleBarStyle(TitleBarStyle.hidden),
            windowManager.setBackgroundColor(CoreStyles.darkGrey5),
          ]);
        }(),
      ]);

      final window = futureResults.first as window_size.PlatformWindow;
      window_size.setWindowMinSize(const Size(800, 600));
      window_size.setWindowFrame(window.screen!.frame);
    }(),
  ]);

  runApp(const AppWidget());
}
